package com.youlai.system.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.system.common.model.Option;
import com.youlai.system.common.result.PageResult;
import com.youlai.system.common.result.Result;
import com.youlai.system.model.entity.SysPeriod;
import com.youlai.system.model.entity.SysScore;
import com.youlai.system.model.query.PeriodPageQuery;
import com.youlai.system.model.vo.PeriodPageVO;
import com.youlai.system.model.vo.PeriodScoreVO;
import com.youlai.system.security.util.SecurityUtils;
import com.youlai.system.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 周期定级控制器
 */
@Tag(name = "19.周期定级接口")
@RestController
@RequestMapping("/api/v1/periods")
@RequiredArgsConstructor
public class SysPeriodController {

    private final SysPeriodService periodService;

    private final SysScoreService scoreService;

    private final SysMeasureService measureService;

    private final SysLocationService locationService;

    private final SysQuestionService questionService;

    @Operation(summary = "周期定级分页列表")
    @GetMapping("/page")
    public PageResult<PeriodPageVO> getScorePage(@ParameterObject PeriodPageQuery queryParams) {
        if (SecurityUtils.isStudent()) {
            queryParams.setLoginUserId(SecurityUtils.getUserId());
        }
        IPage<PeriodPageVO> result = periodService.getPeriodPage(queryParams);
        return PageResult.success(result);
    }

    @Operation(summary = "某个培训司机某个故障周期用的成绩列表数据")
    @GetMapping("/{periodId}/getPrimaryPeriodScoreList")
    public Result<List<PeriodScoreVO>> getPrimaryPeriodScoreList(@Parameter(description = "周期定级ID") @PathVariable Long periodId) {
        SysPeriod period = periodService.getById(periodId);
        String scoreIds = period.getScoreList();
        List<SysScore> list = scoreService.getPrimayPeriodScoreList(period.getStudentId(), StrUtil.split(scoreIds, StrUtil.COMMA).stream().map(Long::valueOf)
                .collect(Collectors.toList()));
        //整改措施
        List<Option> measureList = measureService.listMeasureOptions();
        Map<Long, String> measureMap = measureList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        //培训地点
        List<Option> locationList = locationService.listLocationOptions();
        Map<Long, String> locationMap = locationList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        //问题分类
        List<Option> questionList = questionService.listQuestionOptions(null);
        Map<Long, String> questionMap = questionList.stream().collect(Collectors.toMap(it -> (Long) it.getValue(), Option::getLabel));
        List<PeriodScoreVO> periodScoreVOList = list.stream().map(sysScore -> {
            PeriodScoreVO periodScoreVO = new PeriodScoreVO();

            String measureName = measureMap.get(sysScore.getMeasureId());
            String locationName = locationMap.get(sysScore.getLocationId());
            String questionName = questionMap.get(sysScore.getQuestionId());

            periodScoreVO.setMeasureName(measureName);
            periodScoreVO.setLocationName(locationName);
            periodScoreVO.setQuestionName(questionName);

            periodScoreVO.setCheckDate(sysScore.getCheckDate());
            periodScoreVO.setCheckTime(sysScore.getCheckTime());
            periodScoreVO.setScore(sysScore.getScore());
            periodScoreVO.setQuestionContent(sysScore.getQuestionContent());
            periodScoreVO.setQuestionCount(sysScore.getQuestionCount());
            periodScoreVO.setRemark(sysScore.getContent());
            return periodScoreVO;
        }).collect(Collectors.toList());
        return Result.success(periodScoreVOList);
    }
}
